package org.netty.framework.httpserver.handle;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

/**
 * 定义pipeline,将worker线程处理的channelhandle放到责任链中.
 * 
 * @author chenfanglin
 * @date 2018年9月19日下午6:37:28
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

	/**
	 * 每个请求包的最大大小,默认64K
	 */
	private static final int MAX_CONTENT_LENGTH = 64 * 1024;

	@Override
	protected void initChannel(SocketChannel channel) throws Exception {
		ChannelPipeline pipeline = channel.pipeline();
		// 解码器
		pipeline.addLast(new HttpRequestDecoder());
		pipeline.addLast(new HttpObjectAggregator(MAX_CONTENT_LENGTH));
		// 编码器
		pipeline.addLast(new HttpResponseEncoder());
		/**
		 * 自定义的handle,处理业务逻辑
		 */
		pipeline.addLast(new HttpChannelHandler());
	}

}
